#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include<iostream>
#include<windows.h>
#include <synchapi.h>
#include<string.h>
#include<stdio.h>
using namespace std;




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    socket = new QTcpSocket();
    //连接信号槽
    QObject::connect(socket, &QTcpSocket::readyRead, this, &MainWindow::socket_Read_Data);
    QObject::connect(socket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);

    ui->pushButton_Send->setEnabled(false);
    ui->lineEdit_IP->setText("127.0.0.1");
    ui->lineEdit_Port->setText("65656");

    resize(1200,1200);
    startTimer(500);
    num1=0;
    num2=0;
    num3=0;
    num4=0;

}

int draw_digit(char* str, int* a)
{
    int geshu=0;
    int num = 0;
    for (; *str != '\0'; str++)
    {
        if ((*str >= '0') && (*str <= '9'))
        {
            num = num * 10 + (*str - '0');
            if ((*(str + 1) < '0') || (*(str + 1) > '9'))
            {
                *a = num;
                a++;
                num = 0;
                geshu++;
            }
        }
    }
    return geshu;
}

void MainWindow::paintEvent(QPaintEvent*)
{
    QBrush brush0(Qt::white);
    QBrush brush1(Qt::black);
    QBrush brush2(Qt::red);
    QBrush brush3(Qt::yellow);
    QBrush brush4(Qt::green);
    QBrush brush5(Qt::blue);
    QBrush brush6(Qt::cyan);
    QPen pen(Qt::white);
    QPen pen1(Qt::black);
    QPen pen2(Qt::red);
    QPen pen3(Qt::yellow);
    QPen pen4(Qt::green);
    QPen pen5(Qt::blue);
    QPen pen6(Qt::cyan);


    QPainter Map(this);
    Map.setBrush(brush0);
    Map.setPen(pen);
    if(bmap)
    {
        Map.drawRect(QRect(300,200,30*map[1],30*map[0]));
        Map.setBrush(brush1);
        Map.setPen(pen1);
        for(int i=0;i<map[0];i++)
        {
            for(int j=0;j<map[1];j++)
                if(map[2+map[1]*i+j]==1)
                Map.drawRect(QRect(300+30*j,200+30*i,30,30));
        }
    }



    QPainter Car(this);
    Car.setPen(pen);

    if(bcar1)
    {
     Car.setBrush(brush2);
     Car.drawRect(QRect(30*point1[2]+300,30*point1[1]+200,30,30));
     Car.drawEllipse(QPoint(30*point1[4]+315,30*point1[3]+215),15,15);
        if(num1<n1-1)
        {
            Car.setBrush(brush2);
            Car.drawRect(QRect(30*car1[num1]+315,30*car1[num1+1]+215,30,30));
        }
        else
        {
            Car.setPen(pen2);
            for(int i=0;i<n1-3;i+=2)
            {
                Car.drawLine(30*car1[i]+300,30*car1[i+1]+200,30*car1[i+2]+300,30*car1[i+3]+200);
            }

        }

    }

    if(bcar2)
    {
     Car.setBrush(brush3);
     Car.drawRect(QRect(30*point2[2]+300,30*point3[1]+200,30,30));
     Car.drawEllipse(QPoint(30*point2[4]+315,30*point2[3]+215),15,15);
        if(num2<n2-1)
        {
            Car.setBrush(brush3);
            Car.drawRect(QRect(30*car2[num2]+315,30*car2[num2+1]+215,30,30));
        }
        else
        {
            Car.setPen(pen3);
            for(int i=0;i<n2-3;i+=2)
            {
                Car.drawLine(30*car2[i]+300,30*car2[i+1]+200,30*car2[i+2]+300,30*car2[i+3]+200);
            }

        }

    }

    if(bcar3)
    {
     Car.setBrush(brush4);
     Car.drawRect(QRect(30*point3[2]+300,30*point3[1]+200,30,30));
     Car.drawEllipse(QPoint(30*point3[4]+315,30*point3[3]+215),15,15);
        if(num3<n3-1)
        {
            Car.setBrush(brush4);
            Car.drawRect(QRect(30*car3[num3]+315,30*car3[num3+1]+215,30,30));
        }
        else
        {
            Car.setPen(pen4);
            for(int i=0;i<n3-3;i+=2)
            {
                Car.drawLine(30*car3[i]+300,30*car3[i+1]+200,30*car3[i+2]+300,30*car3[i+3]+200);
            }

        }

    }

    if(bcar4)
    {
     Car.setBrush(brush5);
     Car.drawRect(QRect(30*point4[2]+300,30*point4[1]+200,30,30));
     Car.drawEllipse(QPoint(30*point4[4]+315,30*point4[3]+215),15,15);
        if(num4<n4-1)
        {
            Car.setBrush(brush5);
            Car.drawRect(QRect(30*car4[num4]+315,30*car4[num4+1]+215,30,30));
        }
        else
        {
            Car.setPen(pen5);
            for(int i=0;i<n4-3;i+=2)
            {
                Car.drawLine(30*car4[i]+300,30*car4[i+1]+200,30*car4[i+2]+300,30*car4[i+3]+200);
            }

        }

    }
    if(bcar5)
    {
        Car.setBrush(brush4);
        Car.drawRect(QRect(car5y+300,car5x+200,30,30));
    }

}

void MainWindow::timerEvent(QTimerEvent *)
{
    num1+=2;
    num2+=2;
    num3+=2;
    num4+=2;
    repaint();
}

MainWindow::~MainWindow()
{
    delete this->socket;
    delete ui;
}

void MainWindow::on_pushButton_Connect_clicked()
{
    if(ui->pushButton_Connect->text() == tr("连接"))
    {
        QString IP;
        int port;

        //获取IP地址
        IP = ui->lineEdit_IP->text();
        //获取端口号
        port = ui->lineEdit_Port->text().toInt();

        //取消已有的连接
        socket->abort();
        //连接服务器
        socket->connectToHost(IP, port);

        //等待连接成功
        if(!socket->waitForConnected(30000))
        {
            qDebug() << "Connection failed!";
            return;
        }
        qDebug() << "Connect successfully!";

        //发送按键使能
        ui->pushButton_Send->setEnabled(true);
        //修改按键文字
        ui->pushButton_Connect->setText("断开连接");
    }
    else
    {
        //断开连接
        socket->disconnectFromHost();
        //修改按键文字
        ui->pushButton_Connect->setText("连接");
        ui->pushButton_Send->setEnabled(false);
    }
}

void MainWindow::on_pushButton_Send_clicked()
{
    qDebug() << "Send: " << ui->textEdit_Send->toPlainText();
     //获取文本框内容并以ASCII码形式发送
    socket->write(ui->textEdit_Send->toPlainText().toLatin1());
    socket->flush();
}

void MainWindow::socket_Read_Data()
{
    QByteArray buffer;
    //读取缓冲区数据
    buffer = socket->readAll();
    ch=buffer.data();
    qDebug() << ch;

    if(!buffer.isEmpty())
    {
        QString str = ui->textEdit_Recv->toPlainText();
        str=tr(buffer);
        //刷新显示
        ui->textEdit_Recv->setText(str);
    }
    qDebug() << strlen(ch);
    int num_ch=strlen(ch);
    ch[num_ch]='\0';
    int iint[1600]={0};
    int num_int=draw_digit(ch, iint);
    qDebug()<<num_int<<endl;
    if(ch[0]=='m'&&ch[1]=='a'&&ch[2]=='p')
    {
        for(int i=0;i<num_int;i++)
            map[i]=iint[i];
        bmap=true;

    }
    if(ch[0]=='S'&&ch[1]=='E'&&ch[2]=='T')
    {
        if(ch[4]=='1')
        {
            for(int i=0;i<num_int;i++)
                point1[i]=iint[i];
            bcar1=true;
        }
        if(ch[4]=='2')
        {
            for(int i=0;i<num_int;i++)
                point2[i]=iint[i];
            bcar2=true;
        }
        if(ch[4]=='3')
        {
            for(int i=0;i<num_int;i++)
                point3[i]=iint[i];
            bcar3=true;
        }
        if(ch[4]=='4')
        {
            for(int i=0;i<num_int;i++)
                point4[i]=iint[i];
            bcar4=true;
        }

    }
    if(ch[0]=='S'&&ch[1]=='|'&&ch[3]=='|')
    {
        if(ch[2]=='1')
        {
            for(int i=num_int-1;i>0;i--)
                car1[num_int-1-i]=iint[i];
            bcar1=true;
            n1=num_int-1;
            qDebug()<<n1;
            num1=0;
        }
        if(ch[2]=='2')
        {
            for(int i=num_int-1;i>0;i--)
                car2[num_int-1-i]=iint[i];
            bcar2=true;
            n2=num_int-1;
                        num2=0;
        }
        if(ch[2]=='3')
        {
            for(int i=num_int-1;i>0;i--)
                car3[num_int-1-i]=iint[i];
            bcar3=true;
            n3=num_int-1;
                        num3=0;
        }
        if(ch[2]=='4')
        {
            for(int i=num_int-1;i>0;i--)
                car4[num_int-1-i]=iint[i];
            bcar4=true;
            n4=num_int-1;
                        num4=0;
        }
    }
        if(ch[0]=='M'&&ch[1]=='A'&&ch[2]=='T')
        {
            car5x=iint[0];
            car5y=iint[1];
            bcar5=true;
        }




    //for(int i=0;i<num_int;i++)
    //{
    //    qDebug()<<iint[i]<<endl;
    //}


    /*
    if(ch[0]=='m'&&ch[1]=='a'&&ch[2]=='p')
    {
        for(int i=0;i<600;i++)
        {
            map1[i]=map[i];
        }
    }*/
}

void MainWindow::socket_Disconnected()
{
    //发送按键失能
    ui->pushButton_Send->setEnabled(false);
    //修改按键文字
    ui->pushButton_Connect->setText("连接");
    qDebug() << "Disconnected!";
}
